#include "curve_model.h"
#include <algorithm>

CurveModel::CurveModel(){

}

CurveModel::~CurveModel(){

}

void CurveModel::addPoint(Point point){
	control.push_back(point);
}

void CurveModel::removePoint(Point point){
	Points::iterator it=std::find(control.begin(),control.end(),point);
	if(it!=control.end())
		control.erase(it);
}

void CurveModel::movePoint(Point point,Point futurePoint){
	Points::iterator p=std::find(this->control.begin(),this->control.end(),point);
	this->control.insert(p,futurePoint);
	p=std::find(this->control.begin(),this->control.end(),point);
	this->control.erase(p);
}

Point CurveModel::calculatePoint(float u,float* buffer,unsigned int stride){
	float vu[4] = {0.0f,0.0f,0.0f,1.0f};

	vu[0] = pow(u,3);
	vu[1] = pow(u,2);
	vu[2] = pow(u,1);

	float temp[4][4];
	for(unsigned int r=0;r<3;r++){
		for(unsigned int c=0;c<4;c++){
			float aux = 0;
			for(unsigned int f=0;f<4;f++){
				aux += matrix[c][f]*buffer[(f*stride)+r];
			}
			temp[c][r] = aux;
		}
	}

	Point p;
	for(unsigned int c=0;c<4;c++){
		p.x1 += vu[c]*temp[c][0];
		p.x2 += vu[c]*temp[c][1];
		p.x3 += vu[c]*temp[c][2];
	}

	return p;
}
